<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Config;
use App\Constants\AgreementConstant;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Config(), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->column('id')->sortable();
            $grid->column('mini_type')->display(function ($miniType) use($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('name');
            $grid->column('name_title');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) use($map) {
                $filter->equal('id');
                $filter->equal('mini_type')->select($map);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Config(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('name_title');
            $show->html('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Config(), function (Form $form) {
            $form->display('id');
            $form->select('mini_type')->options(function (){
                return getAuthMiniProgramMap(Admin::user()->id);
            })->required();
            $form->select('name')->options(AgreementConstant::MAP);
            $form->text('name_title');
            $form->editor('content');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
